/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { FileSet } from '@ohos/hvigor';
import * as fse from 'fs-extra';

import { CodeType } from '../../enum/code-type-enum.js';

import { TaskNames } from '../common/task-names.js';
import { TargetTaskService } from '../service/target-task-service.js';
import Task = TaskNames.Task;
import { ProjectConfig } from '@ohos/hvigor-arkts-compose';

import { ArkCompile } from '../ark-compile.js';
import { CommonConst } from '../../const/common-const.js';

/**
 * 处理ohosTest Target时调用loader编译新模型的js/ets源码
 *
 * @since 2023/1/28
 */
export class OhosTestArkCompile extends ArkCompile {
  constructor(taskService: TargetTaskService, codeType: CodeType) {
    super(taskService, codeType, Task.OHOS_TEST_ARK_COMPILE);
  }

  declareInputFiles(): FileSet {
    const fileSet = super.declareInputFiles();

    // ohosTest引用了main中的代码时，main源码也要作为编译增量条件
    if (this.mainCodePath && fse.existsSync(this.mainCodePath!)) {
      fileSet.addEntry(this.mainCodePath!, { isDirectory: true });
    }
    return fileSet;
  }

  protected async initDefaultArkCompileConfig(): Promise<ProjectConfig> {
    const projectConfig = { ...(await super.initDefaultArkCompileConfig()), supportChunks: false };
    if (projectConfig.obfuscationOptions) {
      delete projectConfig.obfuscationOptions;
    }
    if (projectConfig.testFrameworkPar) {
      projectConfig.testFrameworkPar.testMode = CommonConst.OHOS_TEST;
    }

    return projectConfig;
  }
}
